#ifndef BASEHTTPSERVER_INCLUDED
#define BASEHTTPSERVER_INCLUDED

#include "base.h"
#include "BaseTcp.h"
#include "BaseHTTPServerSession.h"

const int  HTTP_REQUEST_SIZE = 4096;

template<typename CLIENT_CONNECTION>
class BaseHttpServer: public BaseAcceptor
{
public:
	BaseHttpServer(Proactor* proactor,unsigned short listenport=BaseHTTPSession::HTTP_PORT):BaseAcceptor(proactor,listenport){};
	~BaseHttpServer(){};
	pj_bool_t OnAccept(Proactor* proactor,pj_sock_t newsock,const pj_sockaddr_t *src_addr,int src_addr_len)
	{
		char buf[100];
		pj_sockaddr_print(src_addr,buf,sizeof(buf)-1,1);
		printf("BaseHttpServer::OnAccept client sock=%d addr=%s\n",newsock,buf);

		CLIENT_CONNECTION *pHttpClientConnection = new CLIENT_CONNECTION(proactor,newsock);
		p_proactor->RegisterHandle(pHttpClientConnection);
		pHttpClientConnection->Recv(HTTP_REQUEST_SIZE);

		return PJ_TRUE;
	};
};

class BASE_API HttpClientConnection:public BaseStream,public BaseHTTPServerSession
{
public:
	HttpClientConnection(Proactor* proactor,pj_sock_t newsock);
	~HttpClientConnection();
protected:
	virtual pj_bool_t OnDataRead(void *data,pj_size_t size,int status,pj_size_t* remainder);
	virtual pj_bool_t OnDataSend(pj_ioqueue_op_key_t *send_key,int sendsize);
protected:
	////subclass implement below virtual method
	//virtual void OnGetRequest(BaseHTTPRequest& request){};
	//virtual void OnPostRequest(BaseHTTPRequest& request){};
	//virtual void OnPutRequest(BaseHTTPRequest& request){};
	//virtual void OnDeleteRequest(BaseHTTPRequest& request){};

};

#endif
